﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using System.Threading;
using gts;

namespace GCard
{
    public partial class IOStatus : Form
    {
        public IOStatus()
        {
            InitializeComponent();
            Initial();
        }
        short sRtn;
        GtsCard card = new GtsCard();
        string tempstr,tempstr1;
        int ipt, opt, home, limp, limn;//输入 输出 原点 正极限 负极限 
        ushort exi, exo;//拓展模块输入 拓展模块输出
        PictureBox[] intputIO = new PictureBox[16];
        PictureBox[] ontputIO = new PictureBox[16];
        PictureBox[] homeIO = new PictureBox[8];
        PictureBox[] pLmtIO = new PictureBox[8];
        PictureBox[] nLmtIO = new PictureBox[8];

        PictureBox[] iEx = new PictureBox[16];//拓展模块输入
        PictureBox[] oEx = new PictureBox[16];//拓展模块输出
        private void Initial()
        {
            this.cmb_mdl.DropDownStyle = ComboBoxStyle.DropDownList;  //模块号初始化
            this.cmb_mdl.Text = this.cmb_mdl.Items[0].ToString();//设置默认值
        }

        Thread threadIO;
        public void IOScan()
        {
            if (threadIO == null)
            {
                threadIO = new Thread(() =>
                {
                    for (int i = 0; i < 16; i++)
                    {
                        tempstr = "pictureBoxI" + (i + 1).ToString();
                        intputIO[i] = (PictureBox)(this.Controls.Find(tempstr, true)[0]);
                        tempstr = "pictureBoxO" + (i + 1).ToString();
                        ontputIO[i] = (PictureBox)(this.Controls.Find(tempstr, true)[0]);
                    }

                    while (true)
                    {
                        sRtn = gts.mc.GT_GetDi(0, mc.MC_GPI, out ipt);
                        sRtn = gts.mc.GT_GetDo(0, mc.MC_GPO, out opt);
                        sRtn = gts.mc.GT_GetDi(0, mc.MC_HOME, out home);
                        sRtn = gts.mc.GT_GetDi(0, mc.MC_LIMIT_POSITIVE, out limp);
                        sRtn = gts.mc.GT_GetDi(0, mc.MC_LIMIT_NEGATIVE, out limn);
                        for (int i = 0; i < 16; i++)
                        {
                            if (((1 << i) & ipt) == 0)
                            {
                                Thread.Sleep(5);
                                intputIO[i].BeginInvoke(new Action(() => { intputIO[i].BackgroundImage = Resource._green; }));
                            }
                            else
                            {
                                Thread.Sleep(5);
                                intputIO[i].Invoke(new Action(() => { intputIO[i].BackgroundImage = Resource._red; }));
                            }

                            if (((1 << i) & opt) == 0)
                            {
                                Thread.Sleep(5);
                                ontputIO[i].Invoke(new Action(() => { ontputIO[i].BackgroundImage = Resource._green; }));
                            }
                            else
                            {
                                Thread.Sleep(5);
                                ontputIO[i].Invoke(new Action(() => { ontputIO[i].BackgroundImage = Resource._red; }));
                            }
                        }
                    }
                });
                threadIO.IsBackground = true;
                threadIO.Start();
            }
        }
      
        Thread threadExtMdlScan;
        ushort pExtstatusValue;
        short mdlNo = 0;//模块号
        public void ExtMdlScan()
        {
            for (int i = 0; i < 16; i++)
            {
                tempstr = "picExI" + (i).ToString();
                iEx[i] = (PictureBox)(this.Controls.Find(tempstr, true)[0]);
                tempstr = "picExO" + (i).ToString();
                oEx[i] = (PictureBox)(this.Controls.Find(tempstr, true)[0]);
            }
            if (threadExtMdlScan == null)
            {
                //cmb_mdl.Invoke(new Action(() =>
                //{
                //    cmb_mdl.Items.Clear();
                //}));
                threadExtMdlScan = new Thread(() =>
                {
                    //for (short i = 0; i < 16; i++)//查找在线模块
                    //{
                    //    sRtn = gts.mc.GT_GetStsExtMdl(0, i, 1, out pExtstatusValue);
                    //    if (pExtstatusValue == 0)
                    //    {
                    //        Thread.Sleep(10);
                    //        cmb_mdl.Invoke(new Action(() =>
                    //        {
                    //            cmb_mdl.Items.Add("模块" + i.ToString());
                    //        }));
                    //    }
                    //}
                    //cmb_mdl.Invoke(new Action(() =>
                    //{
                    //        this.cmb_mdl.Text = this.cmb_mdl.Items[0].ToString();//设置默认值
                    //}));
                    while (true)
                    {
                        cmb_mdl.BeginInvoke(new Action(() =>
                        {
                            mdlNo = Convert.ToInt16(cmb_mdl.SelectedIndex);//查找在线模块
                        }));
                        sRtn = gts.mc.GT_GetExtIoValue(0, mdlNo, out exi);
                        sRtn = gts.mc.GT_GetExtDoValue(0, mdlNo, out exo);
                        for (int i = 0; i < 16; i++)
                        {
                            if (((1 << i) & exi) == 0)
                            {
                                Thread.Sleep(5); 
                                iEx[i].Invoke(new Action(() => { iEx[i].BackgroundImage = Resource._green; }));
                            }
                            else
                            {
                                Thread.Sleep(5);
                                iEx[i].Invoke(new Action(() => { iEx[i].BackgroundImage = Resource._red; }));
                            }
                        }
                        for (int i = 0; i < 16; i++)
                        {
                            if (((1 << i) & exo) == 0)
                            {
                                Thread.Sleep(5);
                                oEx[i].Invoke(new Action(() => { oEx[i].BackgroundImage = Resource._green; }));
                            }
                            else
                            {
                                Thread.Sleep(5);
                                oEx[i].Invoke(new Action(() => { oEx[i].BackgroundImage = Resource._red; }));
                            }
                        }
                    }
                }) { IsBackground = true };
                threadExtMdlScan.Start();
            }
            //else
            //{
            //    threadExtMdlScan.Abort();
            //    cmb_mdl.Invoke(new Action(() =>
            //    {
            //        cmb_mdl.Items.Clear();
            //    }));
            //    threadExtMdlScan = new Thread(() =>
            //    {
            //        for (short i = 0; i < 16; i++)
            //        {
            //            sRtn = gts.mc.GT_GetStsExtMdl(0, i, 1, out pExtstatusValue);
            //            if (pExtstatusValue == 0)
            //            {
            //                Thread.Sleep(10);
            //                cmb_mdl.Invoke(new Action(() =>
            //                {
            //                    cmb_mdl.Items.Add("模块" + i.ToString());
            //                }));
            //            }
            //        }
            //    }) { IsBackground = true };
            //    threadExtMdlScan.Start();
            //}
        }

        private void pictureBoxON(object sender, EventArgs e)
        {
            if (sender == pictureBoxO1)
            {
                card.Output(mc.MC_GPO, 1);

            }
            else if (sender == pictureBoxO2)
            {
                card.Output(mc.MC_GPO, 2);
            }
            else if (sender == pictureBoxO3)
            {
                card.Output(mc.MC_GPO, 3);
            }
            else if (sender == pictureBoxO4)
            {
                card.Output(mc.MC_GPO, 4);
            }
            else if (sender == pictureBoxO5)
            {
                card.Output(mc.MC_GPO, 5);
            }
            else if (sender == pictureBoxO6)
            {
                card.Output(mc.MC_GPO, 6);
            }
            else if (sender == pictureBoxO7)
            {
                card.Output(mc.MC_GPO, 7);
            }
            else if (sender == pictureBoxO8)
            {
                card.Output(mc.MC_GPO, 8);
            }
            else if (sender == pictureBoxO9)
            {
                card.Output(mc.MC_GPO, 9);
            }
            else if (sender == pictureBoxO10)
            {
                card.Output(mc.MC_GPO, 10);
            }
            else if (sender == pictureBoxO11)
            {
                card.Output(mc.MC_GPO, 11);
            }
            else if (sender == pictureBoxO12)
            {
                card.Output(mc.MC_GPO, 12);
            }
            else if (sender == pictureBoxO13)
            {
                card.Output(mc.MC_GPO, 13);
            }
            else if (sender == pictureBoxO14)
            {
                card.Output(mc.MC_GPO, 14);
            }
            else if (sender == pictureBoxO15)
            {
                card.Output(mc.MC_GPO, 15);
            }
            else if (sender == pictureBoxO16)
            {
                card.Output(mc.MC_GPO, 16);
            }
        }

        short dacn;//输出模拟量通道
        short dac;//输人模拟量通道
        //查询模拟量输入
        private void btn_SearchVoltageIn_Click(object sender, EventArgs e)
        {
            double voltage1;//设置输入电压值;
            short voltage2;//设置输入电压转换值;
            dacn = Convert.ToInt16(cmb_adcIn.SelectedIndex);
            sRtn = mc.GT_GetAdc(0, dac, out voltage1, 1, out pClock);
            txt_VoltageInShow.Text = Convert.ToString(voltage1);
            sRtn = mc.GT_GetAdcValue(0, dac, out voltage2, 1, out pClock);//-26214,26214对应-10，+10V 
            txt_VoltageInTFShow.Text = Convert.ToString(voltage2);
        }

        uint pClock;
        private void btn_SearchVoltageOut_Click(object sender, EventArgs e)
        {
            short voltage;//设置输出电压值;
            dacn = Convert.ToInt16(cmb_adcOut.SelectedIndex);
            sRtn = mc.GT_GetDac(0, dacn, out voltage, 1, out pClock);
            txt_VoltageOutShow.Text = Convert.ToString(voltage);
        }

        //本地模拟量电压输出
        private void btn_VoltageOut_Click(object sender, EventArgs e)
        {
            short voltage;//设置输出电压值
            dacn = Convert.ToInt16(cmb_adcOut.SelectedIndex + 1);//通道号
            voltage = Convert.ToInt16(txt_VoltageOut.Text);
            sRtn = mc.GT_SetDac(0, dacn, ref voltage, 1);
        }

        private void ExMdlOn(object sender, EventArgs e)
        {
            
            if (sender == picExO0)
            {
                card.OutputEx(mdlNo, 0);
            }
            else if (sender == picExO1)
            {
                card.OutputEx(mdlNo, 1);
            }
            else if (sender == picExO2)
            {
                card.OutputEx(mdlNo, 2);
            }
            else if (sender == picExO3)
            {
                card.OutputEx(mdlNo, 3);
            }
            else if (sender == picExO4)
            {
                card.OutputEx(mdlNo, 4);
            }
            else if (sender == picExO5)
            {
                card.OutputEx(mdlNo, 5);
            }
            else if (sender == picExO6)
            {
                card.OutputEx(mdlNo, 6);
            }
            else if (sender == picExO7)
            {
                card.OutputEx(mdlNo, 7);
            }
            else if (sender == picExO8)
            {
                card.OutputEx(mdlNo, 8);
            }
            else if (sender == picExO9)
            {
                card.OutputEx(mdlNo, 9);
            }
            else if (sender == picExO10)
            {
                card.OutputEx(mdlNo, 10);
            }
            else if (sender == picExO11)
            {
                card.OutputEx(mdlNo, 11);
            }
            else if (sender == picExO12)
            {
                card.OutputEx(mdlNo, 12);
            }
            else if (sender == picExO13)
            {
                card.OutputEx(mdlNo, 13);
            }
            else if (sender == picExO14)
            {
                card.OutputEx(mdlNo, 14);
            }
            else if (sender == picExO15)
            {
                card.OutputEx(mdlNo, 15);
            }
        }

        int mdl1, mdl2;          //模块种类
        short addr1, addr2;       //地址
        short indexBit1, indexBit2;   //查询
        ushort everyBit;
        ushort status;    //拓展模块状态

        double vol;       //电压值
        double outVol;
        ushort pValueInEx;//拓展模块输入转换值
        double pVelInEx;//拓展模块输入电压值
        ushort pValueOutEx;//拓展模块输出转换值
        double pVelOutEx;//拓展模块输出电压值
        private void btn_VoltageOutEx_Click(object sender, EventArgs e)
        {
            addr2 = Convert.ToInt16(cmb_mdl.SelectedIndex);
            indexBit2 = Convert.ToInt16(cmb_adcOutEx.SelectedIndex);
            pVelOutEx = Convert.ToDouble(txt_VoltageOutEx.Text);
            sRtn = mc.GT_SetExtDaVoltage(0, addr2, indexBit2, pVelOutEx);
        }
        
        private void btn_SearchVoltageInEx_Click(object sender, EventArgs e)
        {
            addr1 = Convert.ToInt16(cmb_mdl.SelectedIndex);
            indexBit1 = Convert.ToInt16(cmb_adcInEx.SelectedIndex);
            sRtn = mc.GT_GetExtAdValue(0, addr1, indexBit1, out pValueInEx);
            sRtn = mc.GT_GetExtAdVoltage(0, addr1, indexBit1, out pVelInEx);
            txt_VoltageInShowEx.Text = Convert.ToString(pVelInEx);
            txt_VoltageInTFShowEx.Text = Convert.ToString(pValueInEx);
        }






        
    }
}
